#include <bsp.h>
#include <nkintr.h>
#include "himemmap.h"

/*
 * Define the LCM panel attribution for CLCDC
 */
/*
LCDTiming0, 0x01010038
0000 0010 0000 0001 0000 0000 0011 1000
HBP = (2 - 1) = 0x01
HFP = (1 - 1) = 0
HSW = (1 - 1) = 0 
PPL = 15
    (16*(PPL + 1) = 16*(15 + 1) = 256)
*/
#define LCD_TIMING_0_HBP    ((0x2 - 1) << 24)    
#define LCD_TIMING_0_HFP    ((0x2 - 1) << 16)   
#define LCD_TIMING_0_HSW    ((0x3 - 1) << 8)     //HWL = 4 DOTCLK
#define LCD_TIMING_0_PPL    ((14 - 0) << 2)  /* 16*(PPL + 1) *///240

/*
LCDTiming1, 0x0100013f
0000 0001 0000 0000 0000 0001 0011 1111
VBP = 1
VFP = 0
VSW = (1 - 1)
LPP = (320 - 1) = 0x13F
*/
#define LCD_TIMING_1_VBP    ((8 - 0) << 24)
#define LCD_TIMING_1_VFP    ((2 - 0) << 16)
#define LCD_TIMING_1_VSW    ((2 - 1) << 10) // VWL >= 1 HSYNC
#define LCD_TIMING_1_LPP    ((320 - 1) << 0)

/*
LCDTiming2, 0x00EF3808
0000 0000 1110 1111 0011 1000 0000 1000
PCD_HI  = 0
BCD     = 0 // not bypass pixel clock divider
CPL     = (240 - 1)
RESERVED
IEO     = 0 // Enable is, 1 - active LOW, 0 - active HIGH
IPC     = 1 // Data is driven, 1 - on the falling edge of CLCP, 0 - on the rising edge
IHS     = 1 // Horizontal is, 1 - active LOW and inactive HIGH, 0 - active HIGH  and inactive LOW
IVS     = 1 // Vertical is, 1 - active LOW and inactive HIGH, 0 - active HIGH  and inactive LOW
ACB     = 0
CLKSEL  = 0
PCD_LO  = 8 // CLCP = CLCDCLK / (PCD + 2)
*/
#define LCD_TIMING_2_PCD_HI         0   //(0 << 27)
#define LCD_TIMING_2_BCD            0   //(0 << 26)
#define LCD_TIMING_2_CPL            ((240 - 1) << 16)
#define LCD_TIMING_2_RESERVED       0   //(0 << 15)   
#define LCD_TIMING_2_IEO            (0 << 14)   //(0 << 14)
#define LCD_TIMING_2_IPC            (1 << 13)
#define LCD_TIMING_2_IHS            (0 << 12)
#define LCD_TIMING_2_IVS            (1 << 11)
#define LCD_TIMING_2_ACB            0   //(0 << 6)
#define LCD_TIMING_2_CLKSEL         0   //(0 << 5)
#define LCD_TIMING_2_PCD_LO         (4 << 0)

/*
LCDTiming3, 0x0

RESERVED
LEE = 0
RESERVED
LED = 0
*/
#define LCD_TIMING_3_LEE            0   //(0 << 16)
#define LCD_TIMING_3_LED            0   //(0 << 0)

/*
LCDControl, 0x00000829
0000 0000 0000 0000 0000 1000 0010 1001

RESERVED
WATERMARK   0
RESERVED
LCDVCOMP    0
LCDPWR      1
BEPO        0
BEBO        0
BGR         0
LCDDUAL     0
LCDMONO8    0
LCDTFT      1
LCDBW       0
LCDBPP      4
LCDEN       0   //  default not enable CLCDC, this bit is controlled by clcdcSwitch function
*/
#define LCD_CONTROL_WATERMARK       0   //(0 << 16)
#define LCD_CONTROL_LCDVCOMP        (0 << 12) //(0 << 12) /* modified by huangjun */
#define LCD_CONTROL_LCDPWR          (1 << 11)
#define LCD_CONTROL_BEPO            0   //(0 << 10)
#define LCD_CONTROL_BEBO            0   //(0 << 9)
#define LCD_CONTROL_BGR             0   //(0 << 8)
#define LCD_CONTROL_LCDDUAL         0   //(0 << 7)
#define LCD_CONTROL_LCDMONO8        0   //(0 << 6)    
#define LCD_CONTROL_LCDTFT          (1 << 5)
#define LCD_CONTROL_LCDBW           0   //(0 << 4)
#define LCD_CONTROL_LCDBPP          (5 << 1)    //RGB666
#define LCD_CONTROL_LCDEN_0         1   //(0 << 0)

/*Define CLCDC registers setting value*/
#define VAL_CLCDC_TIMING_0      ( LCD_TIMING_0_HBP | \
                                  LCD_TIMING_0_HFP | \
                                  LCD_TIMING_0_HSW | \
                                  LCD_TIMING_0_PPL )
                                  
#define VAL_CLCDC_TIMING_1      ( LCD_TIMING_1_VBP | \
                                  LCD_TIMING_1_VFP | \
                                  LCD_TIMING_1_VSW | \
                                  LCD_TIMING_1_LPP )

#define VAL_CLCDC_TIMING_2      ( LCD_TIMING_2_PCD_HI   | \
                                  LCD_TIMING_2_BCD      | \
                                  LCD_TIMING_2_CPL      | \
                                  LCD_TIMING_2_RESERVED | \
                                  LCD_TIMING_2_IEO      | \
                                  LCD_TIMING_2_IPC      | \
                                  LCD_TIMING_2_IHS      | \
                                  LCD_TIMING_2_IVS      | \
                                  LCD_TIMING_2_ACB      | \
                                  LCD_TIMING_2_CLKSEL   | \
                                  LCD_TIMING_2_PCD_LO )

#define VAL_CLCDC_TIMING_3      ( LCD_TIMING_3_LEE | \
                                  LCD_TIMING_3_LED )

#define VAL_CLCDC_CONTROL       ( LCD_CONTROL_WATERMARK |\
                                  LCD_CONTROL_LCDVCOMP  |\
                                  LCD_CONTROL_LCDPWR    |\
                                  LCD_CONTROL_BEPO      |\
                                  LCD_CONTROL_BEBO      |\
                                  LCD_CONTROL_BGR       |\
                                  LCD_CONTROL_LCDDUAL   |\
                                  LCD_CONTROL_LCDMONO8  |\
                                  LCD_CONTROL_LCDTFT    |\
                                  LCD_CONTROL_LCDBW     |\
                                  LCD_CONTROL_LCDBPP    |\
                                  LCD_CONTROL_LCDEN_0 )

const DWORD g_dwTimesPerCs = 3;
const DWORD g_dwLdiDiv = 0x5;

static UINT16 powerOnData1[] = { 0x70, 0x00,0x01, 0x72, 0x01,0x00,
                                 0x70, 0x00,0x02, 0x72, 0x07,0x00,
                                 0x70, 0x00,0x03, 0x72, 0x10,0x30,
                                 0x70, 0x00,0x04, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x08, 0x72, 0x02,0x08,
                                 0x70, 0x00,0x09, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x0A, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x0C, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x0D, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x0F, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x10, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x11, 0x72, 0x00,0x07,
                                 0x70, 0x00,0x12, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x13, 0x72, 0x00,0x00 };
//200ms
static UINT16 powerOnData2[] = { 0x70, 0x00,0x10, 0x72, 0x12,0x90, 
                                 0x70, 0x00,0x11, 0x72, 0x02,0x27 };
//50ms
static UINT16 powerOnData3[] = { 0x70, 0x00,0x12, 0x72, 0x00,0x1A };
//50ms
static UINT16 powerOnData4[] = { 0x70, 0x00,0x13, 0x72, 0x09,0x00,
							     0x70, 0x00,0x29, 0x72, 0x00,0x0b,
                                 0x70, 0x00,0x2B, 0x72, 0x00,0x0D };
//50ms
static UINT16 powerOnData5[] = { 0x70, 0x00,0x20, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x21, 0x72, 0x00,0x00,

                                 0x70, 0x00,0x30, 0x72, 0x00,0x04,
                                 0x70, 0x00,0x31, 0x72, 0x03,0x06,
                                 0x70, 0x00,0x32, 0x72, 0x00,0x03,
                                 0x70, 0x00,0x35, 0x72, 0x03,0x03,
                                 0x70, 0x00,0x36, 0x72, 0x0e,0x06,
                                 0x70, 0x00,0x37, 0x72, 0x04,0x07,
                                 0x70, 0x00,0x38, 0x72, 0x01,0x04,
                                 0x70, 0x00,0x39, 0x72, 0x03,0x07,
                                 0x70, 0x00,0x3C, 0x72, 0x03,0x03,
                                 0x70, 0x00,0x3D, 0x72, 0x04,0x0b,//libra090602
                                      
                                 0x70, 0x00,0x50, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x51, 0x72, 0x00,0xEF,
                                 0x70, 0x00,0x52, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x53, 0x72, 0x01,0x3F,
                                      
                                 0x70, 0x00,0x60, 0x72, 0xA7,0x00,
                                 0x70, 0x00,0x61, 0x72, 0x00,0x01,
                                 0x70, 0x00,0x6A, 0x72, 0x00,0x00,
                                      
                                 0x70, 0x00,0x80, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x81, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x82, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x83, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x84, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x85, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x90, 0x72, 0x00,0x10,
                                 0x70, 0x00,0x92, 0x72, 0x06,0x00,
                                 0x70, 0x00,0x93, 0x72, 0x00,0x03,//libra090602
                                 0x70, 0x00,0x95, 0x72, 0x01,0x10,//libra090602
                                 0x70, 0x00,0x97, 0x72, 0x00,0x00,//libra090602
                                 0x70, 0x00,0x98, 0x72, 0x00,0x00,//libra090602

                                 0x70, 0x00,0x07, 0x72, 0x01,0x33,
                                      
                                 0x70, 0x00,0x20, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x21, 0x72, 0x00,0x00,
                                 0x70, 0x00,0x0F, 0x72, 0x00,0x0A,
                                 0x70, 0x00,0x03, 0x72, 0x10,0x30,
                                 0x70, 0x00,0x0C, 0x72, 0x01,0x10 };
